package com.example.entity.body.impl;

import java.util.Arrays;

import com.example.constant.BodyTypeConstant;
import com.example.constant.MessageConstant;
import com.example.entity.Attachment;
import com.example.entity.body.Body;

import io.netty.buffer.ByteBuf;

public class ASDUBody implements Body {
	private byte type;
	private byte varistructureDet;
	private char sendReason;
	private char addr;
	private Attachment[] attachments = null;
	private int qdsPolicy;
	private int valuePolicy;

	private byte end = MessageConstant.BODY_END_TYPE;

	public ASDUBody() {
		// TODO Auto-generated constructor stub
	}

	public ASDUBody(ByteBuf frame) {
		// TODO Auto-generated constructor stub
		this(frame.readChar(), frame);
	}

	public ASDUBody(char type, ByteBuf frame) {
		// TODO Auto-generated constructor stub
		this(type, frame, MessageConstant.HASVAlUE, MessageConstant.NOQDS);
	}

	/**
	 * @param type
	 * @param frame
	 * @param qdsPolicy
	 */
	public ASDUBody(char type, ByteBuf frame, int valuePolicy, int qdsPolicy) {
		// TODO Auto-generated constructor stub
		setType((byte) type);
		setVaristructureDet(frame.readByte());
		setSendReason(frame.readChar());
		setAddr(frame.readChar());
		setQdsPolicy(qdsPolicy);
		setValuePolicy(valuePolicy);
		if (varistructureDet > 0) {
			attachments = new Attachment[varistructureDet];
			System.out.println("要读" + varistructureDet + "个可变结构限定词");
			for (int i = 0; i < varistructureDet; i++) {

				attachments[i] = new Attachment(frame, (byte) type, valuePolicy, qdsPolicy);

			}

		}
	}

	public final byte getType() {
		return type;
	}

	public final void setType(int type) {
		this.type = (byte) type;
	}

	public final byte getVaristructureDet() {
		return varistructureDet;
	}

	public final void setVaristructureDet(int varistructureDet) {
		this.varistructureDet = (byte) varistructureDet;
	}

	public final char getSendReason() {
		return sendReason;
	}

	public final void setSendReason(int sendReason) {
		this.sendReason = (char) sendReason;
	}

	public final char getAddr() {
		return addr;
	}

	public final void setAddr(int addr) {
		this.addr = (char) addr;
	}

	public final Attachment[] getAttachments() {
		return attachments;
	}

	public final void setAttachments(Attachment[] attachments) {
		this.attachments = attachments;
	}

	public final int getQdsPolicy() {
		return qdsPolicy;
	}

	public final void setQdsPolicy(int qdsPolicy) {
		this.qdsPolicy = qdsPolicy;
	}

	public final int getValuePolicy() {
		return valuePolicy;
	}

	public final void setValuePolicy(int valuePolicy) {
		this.valuePolicy = valuePolicy;
	}

	@Override
	public final int getBodyType() {
		// TODO Auto-generated method stub
		return BodyTypeConstant.ASDU;
	}

	@Override
	public boolean equals(Object obj) {

		if (obj == null)
			return false;
		if (!(obj instanceof ASDUBody)) {
			return false;
		}
		ASDUBody other = (ASDUBody) obj;
		if (addr != other.addr)
			return false;
		if (!Arrays.equals(attachments, other.attachments))
			return false;
		if (sendReason != other.sendReason)
			return false;
		if (type != other.type)
			return false;
		if (varistructureDet != other.varistructureDet)
			return false;
		return true;
	}

	public void write(ByteBuf buf) {

		buf.writeByte(type);
		buf.writeByte(varistructureDet);
		buf.writeChar(sendReason);
		buf.writeChar(addr);
		if (varistructureDet > 0) {
			for (Attachment attachment : attachments) {
				attachment.write(buf);
			}
		}
		buf.writeByte(end);

	}

}
